<?php
// 指定目录
$directory = '/www/wwwroot/theTestFolder'; // 替换为你的目录
$notesFile = 'notes.json';
$notes = file_exists($notesFile) ? json_decode(file_get_contents($notesFile), true) : [];

// 递归函数：展示目录和文件
function displayDirectory($dir, $notes) {
    $files = scandir($dir);
    $directories = [];
    $regularFiles = [];

    // 将目录和文件分开
    foreach ($files as $file) {
        if ($file != '.' && $file != '..') {
            if (is_dir($dir . '/' . $file)) {
                $directories[] = $file;
            } else {
                $regularFiles[] = $file;
            }
        }
    }

    // 按字母顺序排序
    natcasesort($directories);
    natcasesort($regularFiles);

    echo '<ul>';
    // 先展示目录
    foreach ($directories as $directory) {
        $filePath = $dir . '/' . $directory;
        echo "<li class='item'>";
        echo "<div class='directory-info'>";
        echo "<span class='toggle'>[+]</span> <img src='folder-icon.png' alt='Folder' class='icon'> <strong class='folder-name'>$directory</strong>";
        ?>
        <div class="note-container">
            <form class="note-form" data-path="<?php echo htmlspecialchars($filePath); ?>">
                <textarea rows="1" cols="30" class="note-input"><?php echo isset($notes[$filePath]) ? htmlspecialchars($notes[$filePath]) : ''; ?></textarea>
            </form>
        </div>
        <?php
        echo '</div>';
        displayDirectory($filePath, $notes);
        echo '</li>';
    }

    // 再展示文件
    foreach ($regularFiles as $file) {
        $filePath = $dir . '/' . $file;
        echo "<li class='item'>";
        echo "<div class='file-info'>";
        echo "<img src='file-icon.png' alt='File' class='icon'> <span class='filename'>$file</span>";
        ?>
        <div class="note-container">
            <form class="note-form" data-path="<?php echo htmlspecialchars($filePath); ?>">
                <textarea rows="1" cols="30" class="note-input"><?php echo isset($notes[$filePath]) ? htmlspecialchars($notes[$filePath]) : ''; ?></textarea>
            </form>
        </div>
        <?php
        echo '</div>';
        echo '</li>';
    }
    echo '</ul>';
}
?>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FileRemark</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>theTestFolder</h1>
    <?php displayDirectory($directory, $notes); ?>

    <script src="script.js"></script>
</body>
</html>
